package com.whalefin.controller.user;


import com.whalefin.domain.LeaveMessage;
import com.whalefin.domain.User;
import com.whalefin.persistence.tools.PaginationResult;
import com.whalefin.persistence.tools.PaginationUtils;
import com.whalefin.service.LeaveMessageService;
import com.whalefin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
* Created by sf on 14-4-28.
*/
@Controller
@RequestMapping("/searchUser")
public class SearchUserMsgController {

	@Autowired
	private UserService userService;


    /**
     * 根据条件查询用户信息
     * @param pageNo
     * @param model
     * @return
     */
	@RequestMapping(method = RequestMethod.POST)
	public String login(Integer pageNo,Model model,HttpServletRequest request) {

        String username = request.getParameter("username");
        String phoneNum = request.getParameter("phoneNum");

        User user = new User();
        user.setPhoneNum(phoneNum);
        user.setUsername(username);

        //起始页
        if(pageNo == null){
            pageNo = 1;
        }else if(pageNo <= 0){
            pageNo = 1;
        }
        //默认每页显示几行
        int pageSize = PaginationUtils.DEFAULT_PAGESIZE;
        user.setPageSize(pageSize);
        //留言总数
        int totalCount = userService.totalCountByUser(user);
        int count = totalCount/pageSize;
        if(totalCount%pageSize > 0){
            count++;
        }
        if(count != 0 && pageNo >= count){
            pageNo = count;
        }
        int indexPage = PaginationUtils.getStartIndex(pageNo,pageSize);//显示当前页
        user.setIndexPage(indexPage);
        List<User> userList = userService.queryAllUserByUser(user);//分页查询所有留言
        PaginationResult<User> page = new PaginationResult<User>(userList,totalCount,pageSize,indexPage);//分页封装类
        model.addAttribute("page",page);
        return "black/user/userList";
    }
}
